using System;
using System.Timers;
using EstimateTracker.ViewModels;
using JetBrains.Annotations;

namespace EstimateTracker
{
    public class StopToggleTimer : IToggleTimer
    {
        private readonly Timer _t;
        private readonly Action<double> _notifyTimeChanged;
        private readonly Action<double> _notifyTimerStoped;
        private double _time;

        public StopToggleTimer([NotNull] Timer t, [NotNull] Action<double> notifyTimeChanged, [NotNull] Action<double> notifyTimerStoped )
        {
            if (t == null) throw new ArgumentNullException("t");
            if (notifyTimeChanged == null) throw new ArgumentNullException("notifyTimeChanged");
            if (notifyTimerStoped == null) throw new ArgumentNullException("notifyTimerStoped");

            _t = t;
            _notifyTimeChanged = notifyTimeChanged;
            _notifyTimerStoped = notifyTimerStoped;
        }

        public IToggleTimer Toggle()
        {
            _t.Stop();
            _notifyTimerStoped(ToMinutes());
            return new StartToggleTimer(_t, _notifyTimeChanged, _notifyTimerStoped);
        }

        private double ToMinutes()
        {
            return Time/60;
        }

        public double Time
        {
            get { return _time; }
            private set { _time = value; }
        }

        public void Notify(double t)
        {
            Time = t;
            _notifyTimeChanged(ToMinutes());
        }

        public string Label { get { return "Start"; } }
    }
}